<template>
  <el-dialog
    append-to-body
    :title="info+'-店员'"
    :visible.sync="dialogVisible"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="800px">
    <el-form ref="form" :model="form" label-width="180px" :rules="rules">
      <el-form-item label="用户名" align="left" prop="username">
        <el-input v-model="form.username" style="width: 200px" :readonly="form.id>0"></el-input>
      </el-form-item>
      <el-form-item label="头像:" align="left" prop="icon">
        <el-upload
          class="avatar-uploader"
          :action="this.UploadFileUrl"
          :headers="getHeader()"
          name="myUploadFile"
          :show-file-list="false"
          :on-success="uploadIconSuccess">
          <img v-if="form.icon" :src="form.icon" class="avatar" style="width: 80px">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-input v-model="form.icon" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="姓名" align="left" prop="name">
        <el-input v-model="form.name" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="密码" align="left" prop="password">
        <el-input v-model="form.password" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="所属角色" align="left" prop="role">
        <el-select v-model="form.role">
          <el-option v-for="(typeItem,key) in this.roleArr"
                     :key="key"
                     :label="typeItem.name"
                     :value="typeItem.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" align="left" prop="status">
        <el-tag type="info" v-if="form.status==2">已登录锁定</el-tag>
        <el-select v-model="form.status" v-if="form.status!=2">
          <el-option v-for="(value,key) in statusArr1"
                     :key="key"
                     :label="value.name"
                     :value="value.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否聊天客服" align="left" prop="isDefaultChatUser">
        <el-select v-model="form.isDefaultChatUser">
          <el-option v-for="(value,key) in this.statusArr2"
                     :key="key"
                     :label="value.name"
                     :value="value.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
var defaultIcon = 'https://yangfan-shop.oss-cn-hangzhou.aliyuncs.com/icon/avatar.png'
export default {
  username: 'CreateSysUserDialog',
  props: {
    successFunction: Function,
  },
  data: function () {
    return {
      info: '新增',
      dialogVisible: false,
      currLoading: false,
      roleArr: [],
      statusArr1: this.mapToArray(dict.StatusArr),
      statusArr2: this.mapToArray(dict.IsStatusArr),
      form: {
        id: 0,
        username: '',
        name: '',
        password: '',
        status: '1',
        icon: defaultIcon,
        isDefaultChatUser: '1',
        role: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入 用户名', trigger: ['blur', 'change']},
          {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: ['blur', 'change']}
        ],
        name: [
          {required: true, message: '请输入 姓名', trigger: ['blur', 'change']},
          {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: ['blur', 'change']}
        ],
        password: [
          {required: true, message: '请输入 密码', trigger: ['blur', 'change']},
          {min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: ['blur', 'change']}
        ]
      }
    }
  },
  watch: {
    dialogVisible: function () {
      if (!this.dialogVisible) {
        this.form = {
          id: 0,
          username: '',
          name: '',
          password: '',
          status: '1',
          role: '',
          icon: defaultIcon,
          isDefaultChatUser: '1',
        }
      }
    }
  },
  mounted() {
    // this.roleArr = this.mapToArray(dict.UserRoleMap[this.belongUser.userType])
    console.log('this.statusArr', this.statusArr1)
  },
  methods: {
    getRoleArr: function () {
      let $this = this
      $this.MyRequest($this.ApiDoNameShop + 'User/getRoleMap', {userType: 'Shop', checkPremission: 1}, function (data) {
        $this.currLoading = false
        $this.dialogVisible = true
        $this.roleArr = $this.mapToArray(data)
        if (!$this.form.role) {
          $this.form.role = $this.roleArr[0].id
        }
        $this.$forceUpdate()
      }, function () {
        $this.$message.success('操作失败请重试')
        $this.currLoading = false
        $this.$forceUpdate()
      })
    },
    uploadIconSuccess: function (res) {
      this.form.icon = res.data
      this.$forceUpdate()
    },
    open: function (userInfo) {
      this.getRoleArr()
      // this.roleArr = this.mapToArray(dict.UserRoleMap[this.belongUser.userType])
      if (userInfo && userInfo.id > 0) {
        this.info = '更新'
        this.form.id = userInfo.id
        this.form.username = userInfo.username
        this.form.name = userInfo.name
        this.form.role = userInfo.role.id
        this.form.status = userInfo.status.id + ''
        this.form.icon = userInfo.icon
        this.form.isDefaultChatUser = userInfo.isDefaultChatUser.id + ''
        this.rules.password = [
          {min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: ['blur', 'change']}
        ]
        if (this.$refs.form) {
          this.$refs.form.clearValidate()
        }
      } else {
        this.info = '创建'
        this.form = {
          id: 0,
          username: '',
          name: '',
          password: '',
          status: '1',
          role: '',
          icon: defaultIcon,
          isDefaultChatUser: '1',
        }
        this.rules.password = [
          {required: true, message: '请输入 密码', trigger: ['blur', 'change']},
          {min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: ['blur', 'change']}
        ]
        if (this.$refs.form) {
          this.$refs.form.clearValidate()
        }
      }
      this.$forceUpdate()
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          $this.currLoading = true
          var api = 'User/create'
          var form = Object.assign({}, $this.form)
          if (this.form.id > 0) {
            api = 'User/update'
            if (!$this.form.password) {
              delete form.password
            }
          }
          form.userType = 'Shop'
          $this.MyRequest(this.ApiDoNameShop + api, form, function () {
            $this.$message.success($this.info + '成功')
            $this.currLoading = false
            $this.form = {
              id: 0,
              username: '',
              name: '',
              password: '',
              status: '1',
              role: 'Admin',
              icon: defaultIcon,
              isDefaultChatUser: '1',
            }
            $this.dialogVisible = false
            $this.$props.successFunction()
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
